﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using WebHoiChoMuaBan.Controllers.Helpers;

namespace WebHoiChoMuaBan.Models
{
    public partial class UtilityModel:HelperBase
    {
        public string time
        {
            get { return DateTime.Now.ToString("hh:mm | dd.MM.yy"); }
        }

        #region Xử lý phần tiền tệ lấy từ trang VietComBank
        public string exchange_rate
        {
            get
            {
                StringBuilder prices = new StringBuilder();
                try
                {
                    string usd = "";
                    string eur = "";
                    XmlDocument doc = new XmlDocument();
                    WebClient webClient = new WebClient();
                    Stream stream = new MemoryStream(webClient.DownloadData("http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx"));
                    doc.Load(stream);
                    XmlNodeList nodes = doc.GetElementsByTagName("Exrate");
                    foreach (XmlNode node in nodes)
                    {
                        switch (node.Attributes["CurrencyName"].Value)
                        {
                            case "US DOLLAR":
                                {
                                    usd = node.Attributes["Sell"].Value;
                                    break;
                                }
                            case "EURO":
                                {
                                    eur = node.Attributes["Sell"].Value;
                                    break;
                                }
                        }
                    }
                    return prices.Append("USD = " + usd + " VND, EUR = " + eur + " VND").ToString();
                }
                catch (Exception e)
                {
                    return prices.Append("Không lấy được dữ liệu...").ToString();
                    throw;
                }
            }
        }
        #endregion

        public List<Province> ListProvince()
        {
            UtilityHelper utilityHelper = new UtilityHelper();
            return utilityHelper.GetListProvince();
        }
    }
}